home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 6 / The Arsenal Files 6 (Arsenal Computer).ISO / os2 / am4pmsrc.zip / AM4PMCMD.C < prev    next >
C/C++ Source or Header  |  1993-10-13  |  3KB  |  144 lines

  1. // Thomas Answering machine for PM
  2.  
  3. // File:          AM4PMCMD.c
  4. // Description:   Program form sending commands to AM4PM
  5.  
  6. // History
  7. // 930517 TO      Now it exists...
  8. // 930531 TO      New command 'c'
  9.  
  10.  
  11. #define INCL_BASE
  12.  
  13. #include <os2.h>
  14.  
  15. #include <stddef.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20.  
  21. #include "am4pm.h"
  22.  
  23. static CHAR szVer[]="v0.1b (31 May 1993)";
  24. static CHAR szExtQueue[]="\\QUEUES\\AM4PM";
  25. static CHAR szSemCom[]="\\SEM32\\AM4PM\\COM";
  26.  
  27. static int SendExtQMsg
  28. (
  29.    USHORT usEvent,
  30.    PVOID pData,
  31.    ULONG ulLen
  32. )
  33. {
  34.    USHORT res;
  35.    ULONG ulPID;
  36.    HQUEUE hQueue;
  37.    PVOID pItem;
  38.  
  39.    res=DosOpenQueue(&ulPID, &hQueue, szExtQueue);
  40.    if (res)
  41.    {
  42.       printf("AM4PM not running\n");
  43.       return 2;
  44.    }
  45.  
  46.    if (pData != NULL)
  47.    {
  48.       DosAllocSharedMem(&pItem, NULL, ulLen, PAG_COMMIT | OBJ_GIVEABLE | PAG_READ | PAG_WRITE);
  49.       memcpy(pItem, pData, ulLen);
  50.       DosGiveSharedMem(pItem, ulPID, PAG_READ | PAG_WRITE);
  51.       DosFreeMem(pItem);
  52.    }
  53.    else
  54.       pItem=NULL;
  55.  
  56.    res=DosWriteQueue(hQueue, usEvent, ulLen, pItem, 0);
  57.  
  58.    DosCloseQueue(hQueue);
  59.    if (res)
  60.       return 2;
  61.    return 0;
  62. }
  63.  
  64.  
  65. static int RequestCom(void)
  66. {
  67.    USHORT res;
  68.    HMTX sem=0;
  69.  
  70.    printf("Sending message to AM4PM to release COM port\n");
  71.    res=SendExtQMsg(EQ_RELCOM, NULL, 0);
  72.    if (res)
  73.       return res;
  74.  
  75.    res=DosOpenMutexSem(szSemCom, &sem);
  76.    if (res)
  77.    {
  78.       printf("Error %u opening semaphore\n", res);
  79.       return 3;
  80.    }
  81.  
  82.    res=DosRequestMutexSem(sem, 120000l);
  83.    if (res)
  84.    {
  85.       DosCloseMutexSem(sem);
  86.       if (res==ERROR_TIMEOUT)
  87.       {
  88.          printf("Timeout waiting for AM4PM to release COM port\n");
  89.          return 4;
  90.       }
  91.       printf("Error %u waiting for semaphore\n", res);
  92.       return 3;
  93.    }
  94.  
  95.    DosReleaseMutexSem(sem);
  96.    DosCloseMutexSem(sem);
  97.    return 0;
  98. }
  99.  
  100.  
  101. int main
  102. (
  103.    USHORT usArgc,
  104.    PCHAR pchArgv[]
  105. )
  106. {
  107.    printf("AM4PMCMD %s\n\n", szVer);
  108.  
  109.    if (usArgc <= 1)
  110.    {
  111.       printf("Usage: AM4PMCMD cmd <p1>\n");
  112.       printf("\tcmd r\trelease COM port for 30 s\n");
  113.       printf("\t    c\tstart AMC file p1\n");
  114.       printf("\t    u\tsend user defined message p1\n");
  115.       return 1;
  116.    }
  117.  
  118.    switch (tolower(pchArgv[1][0]))
  119.    {
  120.    case 'r':
  121.       return RequestCom();
  122.  
  123.    case 'c':
  124.       if (usArgc <= 2)
  125.       {
  126.          printf("Too few parameters\n");
  127.          return 1;
  128.       }
  129.       return SendExtQMsg(EQ_STARTAMC, pchArgv[2], strlen(pchArgv[2])+1);
  130.  
  131.    case 'u':
  132.       if (usArgc <= 2)
  133.       {
  134.          printf("Too few parameters\n");
  135.          return 1;
  136.       }
  137.       return SendExtQMsg(EQ_USER, pchArgv[2], strlen(pchArgv[2])+1);
  138.  
  139.    default:
  140.       printf("Unkown command '%c'\n", tolower(pchArgv[1][0]));
  141.       return 1;
  142.    }
  143. }
  144.